package com.vision.admin.company.cl.ui;

import static com.vision.admin.cl.AdminServices.companyGroupSvc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.vision.admin.cl.resource.AdminResources;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.event.CompanyGroupCreatedEvent;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.util.FieldUtils;
import com.vision.core.cm.db.data.Company;
import com.vision.core.cm.db.data.CompanyGroup;

/**
 * 
 * @author Mark
 *
 */
public class CreateCompanyGroupWindow implements IsWidget, Editor<CompanyGroup> {
	
	interface Binder extends UiBinder<Widget, CreateCompanyGroupWindow> { }
	interface CompanyDriver extends SimpleBeanEditorDriver<CompanyGroup, CreateCompanyGroupWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer formContainer;
	
	@UiField TextField code;
	@UiField TextField description;
	
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;

	@Ignore @UiField Window window;
	private Company company;
	
	private CompanyDriver driver = GWT.create(CompanyDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public CreateCompanyGroupWindow(Company company) {
		this.company = company;
	}

	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(AdminResources.INSTANCE.companies());
		
		// Add validators to fields
		FieldUtils.maxOf(code, 1);
		FieldUtils.minMaxOf(code, 1, Company.FieldSpecs.CODE_MAX);
		FieldUtils.minMaxOf(description, 1, Company.FieldSpecs.NAME_MAX);
		
		driver.initialize(this);
		CompanyGroup companyGroup = new CompanyGroup();
		companyGroup.setCompany(company);
		
		driver.edit(companyGroup);
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		CompanyGroup companyGroup = driver.flush();
		if (driver.hasErrors() || companyGroup == null)
			return;
		
		companyGroup.setCode(companyGroup.getCode().toUpperCase());
		
		UI.mask(window, "Creating company group...");
		companyGroupSvc().create(companyGroup, new Callback<CompanyGroup>(window) {
			@Override
			protected void handleSuccess(CompanyGroup result) {
				window.hide(saveBtn);
				BaseModule.fireEvent(new CompanyGroupCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}
	
	// Helper Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

}
